package com.atguigu.dao.impl;

import com.atguigu.Bean.C_book;
import com.atguigu.dao.BookDao;

import java.math.BigDecimal;
import java.util.List;

public class BookDaoImpl extends BaseDao implements BookDao {
    @Override
    public int addbook(C_book book) {
        String sql="insert into c_books values(null,?,?,?,?,?,?)";
        String name = book.getName();
        BigDecimal price = book.getPrice();
        String author = book.getAuthor();
        int sales = book.getSales();
        int stock = book.getStock();
        String img_path = book.getImg_path();
        return updatedate(sql,name,price,author,sales,stock,img_path);
    }

    @Override
    public int deleteBookById(int id) {
        String sql="delete from c_books where id=?";
        return updatedate(sql,id);
    }

    @Override
    public int updateBook(C_book book) {
        String sql="update c_books set `name`=?,`price`=?,`author`=?,`sales`=?,`stock`=?,`img_path`=? where id=?";
        String name = book.getName();
        BigDecimal price = book.getPrice();
        String author = book.getAuthor();
        int sales = book.getSales();
        int stock = book.getStock();
        String img_path = book.getImg_path();
        int id = book.getId();
        return updatedate(sql,name,price,author,sales,stock,img_path,id);
    }

    @Override
    public C_book queryBookById(int id) {
        String sql="select `id`,`name`,`price`,`author`,`sales`,`stock`,`img_path` from c_books where id=?";
        return querydate(C_book.class,sql,id);
    }

    @Override
    public List<C_book> queryBooks() {
        String sql="select `id`,`name`,`price`,`author`,`sales`,`stock`,`img_path` from c_books";
        return querydates(C_book.class,sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql="select count(*) from c_books";
        return Integer.parseInt(String.valueOf(queryvalue(sql)));
    }

    @Override
    public List<C_book> queryForPageItems(int begin, int pageSize) {
        String sql="select * from c_books limit ?,?;";
        return querydates(C_book.class,sql,begin,pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByPrice(int min, int max) {
        String sql="select count(*) from c_books where price between ? and ?";
        return Integer.parseInt(String.valueOf(queryvalue(sql,min,max))) ;
    }

    @Override
    public List<C_book> queryForPageItemsByPrice(int begin, int pageSize, int min, int max) {
        String sql="select * from c_books where price between ? and ? order by price asc limit ?,?";
        return querydates(C_book.class,sql,min,max,begin,pageSize);
    }
}
